/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import Model.Shape;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.LinkedList;
import javax.swing.JPanel;

/**
 *
 * @author PhucAnh
 */
public class MyPanel extends JPanel{
    
    public Graphics bufferGraphics = null;
    public Image bufferImage = null;
    private LinkedList<Shape> myObject;
    
    public MyPanel(LinkedList<Shape> myObject) {
        this.myObject = myObject;
    }
    public MyPanel(LinkedList<Shape> myObject,Image bufferImage) {
        this.myObject = myObject;
        this.bufferImage=bufferImage;
    }
    
    @Override
    public void paintComponent(Graphics g) {
         //super.paintComponent(g);
         Graphics2D g2 = (Graphics2D) g;

         if (bufferImage == null) {
                bufferImage = createImage(this.getSize().width, this.getSize().height);
                bufferGraphics = bufferImage.getGraphics();
            }
            // clear the off-screen
            bufferGraphics.setColor(Color.white);
            bufferGraphics.fillRect(0, 0, this.getSize().width, this.getSize().height);
            // draw game elements on off-screen
            // put the off-screen on the main screen
            //            Dimension screen_size;
            //            //screen_size = Toolkit.getDefaultToolkit().getScreenSize();
            //            screen_size = jpnPaint.getSize();
            //            Rectangle rect = new Rectangle(jpnPaint.getLocationOnScreen().x,jpnPaint.getLocationOnScreen().y,(int)screen_size.getWidth(),(int)screen_size.getHeight());
            //            bufferImage = robot.createScreenCapture(rect);
            g2.drawImage(bufferImage, 0, 0, this);
            for(int i=0; i<myObject.size();i++) {
            myObject.get(i).drawShape(g,(BufferedImage)bufferImage);
        }
}
}
